package com.yonyou.jrmob.application.common.adapter;

import android.content.Context;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.yonyou.jrmob.application.R;
import java.util.List;

import jrmob.yonyou.com.commonlib.app_config_tool.constant.Constant;
import jrmob.yonyou.com.commonlib.vo.AppVO_ap;

public class AppGridAdapter extends BaseAdapter {
    private List<AppVO_ap> appBeanList;
    private Context context;

    public AppGridAdapter(Context context, List<AppVO_ap> data) {
        this.context = context;
        appBeanList = data;
    }

    @Override
    public int getCount() {
        return appBeanList.size();
    }

    @Override
    public AppVO_ap getItem(int position) {
        return appBeanList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        AppViewHolder holder = null;
        AppVO_ap bean = appBeanList.get(position);
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_app_list, null);
            holder = new AppViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (AppViewHolder) convertView.getTag();
        }
        String appName = TextUtils.isEmpty(bean.getAppname()) ? bean.getApp_name() : bean.getAppname();
        holder.name.setText(appName);
        if ("全部".equals(appName)) {
            Glide.with(context)
                    .load(R.drawable.icon_all)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .crossFade()
                    .into(holder.icon);
        } else if ("工作日历".equals(appName)) {
            Glide.with(context)
                    .load(R.drawable.icon_calander)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .crossFade()
                    .into(holder.icon);
        } else if (Constant.EXTRA_H5_APP.equals(appName)) {
            Glide.with(context)
                    .load(R.drawable.home_mifeng)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .crossFade()
                    .into(holder.icon);
        }  else if (Constant.EXTRA_H5_YIDONGSHENPI.equals(appName)) {
            Glide.with(context)
                    .load(R.drawable.home_mifeng)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .crossFade()
                    .into(holder.icon);
        } else if (Constant.EXTRA_H5_CHEEGU.equals(appName)) {
            Glide.with(context)
                    .load(R.drawable.icon_calander)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .centerCrop()
                    .crossFade()
                    .into(holder.icon);
        } else {
            if (null != bean.getWebiconurl()){
                Glide.with(context)
                        .load(Uri.parse(bean.getWebiconurl()))
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .crossFade()
                        .into(holder.icon);
            } else if (null != bean.getLogo_url()){
                Glide.with(context)
                        .load(Uri.parse(bean.getLogo_url()))
                        .diskCacheStrategy(DiskCacheStrategy.ALL)
                        .crossFade()
                        .into(holder.icon);
            }
        }
        return convertView;
    }

    class AppViewHolder {
        private ImageView icon;
        private TextView name;

        public AppViewHolder(View itemView) {
            icon = (ImageView) itemView.findViewById(R.id.item_app_list_icon);
            name = (TextView) itemView.findViewById(R.id.item_app_list_name);
        }
    }
}